package com.priox.sigar.jsf;


import com.priox.sigar.application.mappers.CheckPointMapper;
import com.priox.sigar.client.dto.CheckPointDTO;
import com.priox.sigar.domain.model.AgrupacionRepository;
import com.priox.sigar.domain.model.constantes.PathConstant;
import com.priox.sigar.domain.service.ComunQryService;
import com.priox.sigar.dto.CheckPointFlt;
import com.priox.sigar.enums.ProcesoEstado;
import com.priox.sigar.enums.TipoCuenta;
import com.priox.sigar.jsf.util.FindCrudBeanBase;
import com.priox.sigar.server.infrastructure.persistence.exception.ApplicationException;
import com.priox.sigar.server.infrastructure.persistence.util.ApplicationProperties;
import org.primefaces.event.CloseEvent;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.event.SelectEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.event.ActionEvent;
import java.util.*;

@ManagedBean
@Scope("session")
@Component
public class MainJsfBean extends FindCrudBeanBase {
    private static final Logger LOG = LoggerFactory.getLogger(MainJsfBean.class);

    //================================================================================
    // Spring Managed Beans
    //================================================================================
    @Autowired
    protected ComunQryService comunQryService;

    @Autowired
    protected CheckPointMapper checkPointMapper;

    @Autowired
    protected AgrupacionRepository agrupacionRepository;

    CheckPointDTO checkPointSeleccionado;


    //================================================================================
    // Form Data
    //================================================================================
    private Long agrupacionId;
    private List<CheckPointDTO> registros;

    @PostConstruct
    public void init() {
        PathConstant.DIR_ROOT = ApplicationProperties.getDirectorio();
        PathConstant.DIR_TMP = PathConstant.DIR_ROOT + "\\tmp";
        PathConstant.DIR_JASPER= PathConstant.DIR_ROOT + "\\jasper";
        CheckPointFlt map = generarFiltro();
        registros = checkPointMapper.findLast(map);
    }

    private CheckPointFlt generarFiltro() {
        CheckPointFlt checkPointFlt=new CheckPointFlt();
        checkPointFlt.setUsuarioId(getUser().getUsuarioId());
        checkPointFlt.setTipoCuenta(null);
        checkPointFlt.setEstado(ProcesoEstado.TERMINADO);
        return checkPointFlt;
    }


    @Override
    public void setCursorTableNull() {

    }

    @Override
    public void onClean(ActionEvent actionEvent) {
        registros = new ArrayList<CheckPointDTO>();

    }

    @Override
    public void onNewRecord(ActionEvent actionEvent) {

    }

    @Override
    public void onShowEditDialog(ActionEvent actionEvent) {

        if(selectCheckPoint==null) throw new ApplicationException("Seleccione un checkpoint");

        if (TipoCuenta.CUENTA_X_PAGAR.equals(selectCheckPoint.getTipo())){
            navigateTo("reporteCuentaPagar.jsf");
        }else if (TipoCuenta.CUENTA_X_COBRAR.equals(selectCheckPoint.getTipo())) {

            navigateTo("reporteCuentaCobrar.jsf");
        } else {
            navigateTo("reporteCuentaAlmacen.jsf");
        }

    }

    public void onFind(ActionEvent actionEvent) {
        CheckPointFlt map = generarFiltro();
        registros = checkPointMapper.findLast(map);
    }

    @Override
    public void onPersist(ActionEvent actionEvent) {

    }


    @Override
    protected Map onPrintParameter(String templateFileName,ActionEvent actionEvent) {
        Map result = new HashMap();
        result.put("fechaGeneracion", new Date());
        return result;
    }

    @Override
    protected List onRegistrosList(String templateFileName,ActionEvent actionEvent) {
        return null;
    }

    @Override
    public void onDelete(ActionEvent actionEvent) {


    }

    private CheckPointDTO selectCheckPoint;
    public void onSelectDetail(SelectEvent actionEvent) {
        selectCheckPoint = (CheckPointDTO) getSelectedRowTable("tablaLocales");

    }

    public String onDownloadFile(ActionEvent actionEvent) {

        return null;
    }


    public void onFileUpload(FileUploadEvent event) {

    }

    @Override
    protected void onDialogClose(CloseEvent event) {

        LOG.debug("Closing CRUD Dialog for Periodo");

    }

    //================================================================================
    // Autocompleters
    //================================================================================


    //================================================================================
    // Accessors
    //================================================================================


    public List<CheckPointDTO> getRegistros() {
        return registros;
    }

    public void setRegistros(List<CheckPointDTO> registros) {
        this.registros = registros;

    }



    public Long getAgrupacionId() {
        return agrupacionId;
    }

    public void setAgrupacionId(Long agrupacionId) {
        this.agrupacionId = agrupacionId;
    }

    public CheckPointDTO getCheckPointSeleccionado() {
        return checkPointSeleccionado;
    }

    public void setCheckPointSeleccionado(CheckPointDTO checkPointSeleccionado) {
        this.checkPointSeleccionado = checkPointSeleccionado;
    }

    public AgrupacionRepository getAgrupacionRepository() {
        return agrupacionRepository;
    }

    public void setAgrupacionRepository(AgrupacionRepository agrupacionRepository) {
        this.agrupacionRepository = agrupacionRepository;
    }

}
